package server.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

import server.core.ServerDispatcher;

public class ClientListener extends Thread {

        private Client client;
        private ServerDispatcher dispatcher;
        private BufferedReader inputReader;

        public ClientListener(Client client, ServerDispatcher dispatcher) throws IOException {
                this.client = client;
                this.dispatcher = dispatcher;
               
                Socket socket = client.getSocket();
                this.inputReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        }
       
        /*
         * Liest Nachrichten vom Client
         * (non-Javadoc)
         * @see java.lang.Thread#run()
         */
        @Override
        public void run() {
                try {
                        while(!isInterrupted()) {
                                String message = this.inputReader.readLine();
                                if (message != null) {
                                        this.dispatcher.dispatchMessage(this.client, message);
                                }
                        }
                } catch (IOException ex) {
                        // Kann nicht vom Socket lesen
                }
               
                // Problem mit der Verbindung
                this.client.getClientSender().interrupt();
                this.dispatcher.deleteClient(this.client);
        }
}

